<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Game extends CI_Controller {


	public function index()
	{
		//
		
		$config['base_url'] = 'game/';
		$config['total_rows'] = $this->db->get('game')->num_rows();
		$config['per_page'] = 5;
		$config['num_links'] = 5;
		$config['full_tag_open'] = '<div class="paginaton"><ul>';
		$config['full_tag_close'] = '</ul></div>';
		$config['first_link'] = false;
		$config['last_link'] = false;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev">';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next $rarr;';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="#">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$this->pagination->initialize($config);			
		//
		$data['records'] = $this->db->get('game',$config['per_page'],$this->uri->segment(3)); 
		$data['title'] = 'New Paltz - Join a Game';
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('home_view');
		$this->load->view('includes/footer_view');
	}
	public function _joinGame()
	{
		$this->load->library("form_validation");		
		$this->form_validation->set_rules("user_id","User Id","trim|required|xss_clean");
	    $this->form_validation->set_rules("game_id","Game Id","trim|required|xss_clean");
		
		if($this->form_validation->run() == FALSE)
		{
			//$this->go back to game table
		}
		else 
		{
			$this->load->model("Game_model");
			$user_id = $this->input->post('user_id');
			$game_id = $this->input->post('game_id');
	 		$killcode = _randomString(8);
	 		$this->Game_model->joined_game($user_id, $game_id, $killcode);
	 		send messaging saying they joined the game they picked 		
		}
	}
	
	function _randomString($length)
	{
		$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		$string ="";
		for($i=0;$i<$length;$i++)
			$string .= $characters[rand(0, strlen($characters) - 1)];
		return $string;
	}
}

/* End of file game.php */
/* Location: ./application/controllers/game.php */